import service from '@/utils/request.js'
import { useClassIdStore } from '@/stores/classId.js'
import { useSignOrderStore } from '@/stores/signOrder'
import { useSignIdStore } from '@/stores/signId'

const signIdStore = useSignIdStore();
const classIdStore = useClassIdStore();
const signOrderStore = useSignOrderStore();
export const createSign = async (duration) => {
  const classId = classIdStore.classId;

  console.log('Sending sign request with classId:', classId, 'duration:', duration);
  // 添加参数验证
  if (!classId) {
      console.error('classId is missing or invalid');
      throw new Error('classId is missing or invalid');
  }
  if (typeof duration !== 'number' || duration <= 0) {
      console.error('duration is missing or invalid');
      throw new Error('duration is missing or invalid');
  }
  console.log('Sending sign request with classId:', classId, 'duration:', duration); // 调试信息
  try {
      // 发起请求
      const response = await service.post('/sign/start', null, {
          params: {
              classId,
              duration
          }
      });
      console.log('1sign.js后端返回的完整数据：', response); // 添加调试信息
      // 从响应数据中提取 signOrder
      const signOrder = response.signOrder;
      console.log('Sign order:', signOrder);
      signOrderStore.setSignOrder(signOrder);
      const signId = response.signId;
      if (signId === undefined) {
          console.error('后端返回的数据中没有 signId 字段');
          throw new Error('后端返回的数据中没有 signId 字段');
      }
      signIdStore.setSignId(signId);//存sginId
      // console.log('signId:', signId);
      // console.log('Store里面的signId:', signIdStore.signId);

      return response;
  } catch (error) {
      console.error('发起签到请求时出错:', error);
      throw error;
  }
}
// 提交签到
export const submitSign = async (signId, userId, code, courseManageId) => {
  try {
    await service.post('/sign/submit', null, {
      params: {
        signId,
        userId,
        code,
        courseManageId
      }
    });
    console.log('签到提交成功');
  } catch (error) {
    console.error('签到提交失败:', error);
    throw error;
  }
}
//获取签到记录
export const getStatus = async (signId) => {
  try {
    const response = await service.get(`/sign/status/${signId}`);
    console.log('当前签到记录获取成功:', response);
    return response;
  } catch (error) {
    console.error('当前签到记录获取失败:', error);
    throw error;
  }
}

export const getSignStatistics = async (courseManageId) => {

  const response = await service.get(`/sign/statistics`,{params:{courseManageId}});//没有null，不需要传递请求体数据

  console.log('签到统计获取成功:', response);
  return response;
 
}


export const getStudentAllSign = async (userId,courseManageId) => {
try{
  const response = await service.get(`/sign/getStudentAllSign`,{params:{userId,courseManageId}});
  console.log('签到统计获取成功:', response);
  return response;
}catch(error){
  console.error('签到统计获取失败:', error);
  throw error;
}
 
}

export const getSignId = async(courseManageId)=>{
  try{
    const response = await service.get('/sign/latestSignId',{params:{courseManageId}});
  console.log('获取签到id成功:', response);
  return response;
}catch(error){
  console.error('获取签到id失败:', error);
  throw error;
}
}
